// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.objects;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.characters.IsoGameCharacter;
import zombie.core.Color;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.inventory.types.DrainableComboItem;
import zombie.inventory.types.Moveable;
import zombie.iso.IsoCell;
import zombie.iso.IsoChunk;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoLightSource;
import zombie.iso.IsoObject;
import zombie.iso.IsoRoomLight;
import zombie.iso.IsoWorld;
import zombie.iso.areas.IsoRoom;
import zombie.iso.sprite.IsoSprite;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.PacketTypes;

public class IsoLightSwitch extends IsoObject {
    boolean Activated = false;
    public final ArrayList<IsoLightSource> lights = new ArrayList();
    public boolean lightRoom = false;
    public int RoomID = -1;
    public boolean bStreetLight = false;
    private boolean canBeModified = false;
    private boolean useBattery = false;
    private boolean hasBattery = false;
    private String bulbItem = "Base.LightBulb";
    private float power = 0.0F;
    private float delta = 2.5E-4F;
    private float primaryR = 1.0F;
    private float primaryG = 1.0F;
    private float primaryB = 1.0F;
    protected long lastMinuteStamp = -1L;
    protected int bulbBurnMinutes = -1;
    protected int lastMin = 0;
    protected int nextBreakUpdate = 60;

    @Override
    public String getObjectName() {
        return "LightSwitch";
    }

    public IsoLightSwitch(IsoCell cell) {
        super(cell);
    }

    public IsoLightSwitch(IsoCell cell, IsoGridSquare sq, IsoSprite gid, int _RoomID) {
        super(cell, sq, gid);
        this.RoomID = _RoomID;
        if (gid != null && gid.getProperties().Is("lightR")) {
            if (gid.getProperties().Is("IsMoveAble")) {
                this.canBeModified = true;
            }

            this.primaryR = Float.parseFloat(gid.getProperties().Val("lightR")) / 255.0F;
            this.primaryG = Float.parseFloat(gid.getProperties().Val("lightG")) / 255.0F;
            this.primaryB = Float.parseFloat(gid.getProperties().Val("lightB")) / 255.0F;
        } else {
            this.lightRoom = true;
        }

        this.bStreetLight = gid != null && gid.getProperties().Is("streetlight");
        IsoRoom isoRoom = this.square.getRoom();
        if (isoRoom != null && this.lightRoom) {
            if (!sq.haveElectricity() && !IsoWorld.instance.isHydroPowerOn()) {
                isoRoom.def.bLightsActive = false;
            }

            this.Activated = isoRoom.def.bLightsActive;
            isoRoom.lightSwitches.add(this);
        } else {
            this.Activated = true;
        }
    }

    public void addLightSourceFromSprite() {
        if (this.sprite != null && this.sprite.getProperties().Is("lightR")) {
            float float0 = Float.parseFloat(this.sprite.getProperties().Val("lightR")) / 255.0F;
            float float1 = Float.parseFloat(this.sprite.getProperties().Val("lightG")) / 255.0F;
            float float2 = Float.parseFloat(this.sprite.getProperties().Val("lightB")) / 255.0F;
            this.Activated = false;
            this.setActive(true, true);
            int _int = 10;
            if (this.sprite.getProperties().Is("LightRadius") && Integer.parseInt(this.sprite.getProperties().Val("LightRadius")) > 0) {
                _int = Integer.parseInt(this.sprite.getProperties().Val("LightRadius"));
            }

            IsoLightSource isoLightSource = new IsoLightSource(this.square.getX(), this.square.getY(), this.square.getZ(), float0, float1, float2, _int);
            isoLightSource.bActive = this.Activated;
            isoLightSource.bHydroPowered = true;
            isoLightSource.switches.add(this);
            this.lights.add(isoLightSource);
        }
    }

    public boolean getCanBeModified() {
        return this.canBeModified;
    }

    public float getPower() {
        return this.power;
    }

    public void setPower(float _power) {
        this.power = _power;
    }

    public void setDelta(float _delta) {
        this.delta = _delta;
    }

    public float getDelta() {
        return this.delta;
    }

    public void setUseBattery(boolean b) {
        this.setActive(false);
        this.useBattery = b;
        if (GameClient.bClient) {
            this.syncCustomizedSettings(null);
        }
    }

    public boolean getUseBattery() {
        return this.useBattery;
    }

    public boolean getHasBattery() {
        return this.hasBattery;
    }

    public void setHasBatteryRaw(boolean b) {
        this.hasBattery = b;
    }

    public void addBattery(IsoGameCharacter chr, InventoryItem battery) {
        if (this.canBeModified && this.useBattery && !this.hasBattery && battery != null && battery.getFullType().equals("Base.Battery")) {
            this.power = ((DrainableComboItem)battery).getUsedDelta();
            this.hasBattery = true;
            chr.removeFromHands(battery);
            chr.getInventory().Remove(battery);
            if (GameClient.bClient) {
                this.syncCustomizedSettings(null);
            }
        }
    }

    public DrainableComboItem removeBattery(IsoGameCharacter chr) {
        if (this.canBeModified && this.useBattery && this.hasBattery) {
            DrainableComboItem drainableComboItem = (DrainableComboItem)InventoryItemFactory.CreateItem("Base.Battery");
            if (drainableComboItem != null) {
                this.hasBattery = false;
                drainableComboItem.setUsedDelta(this.power >= 0.0F ? this.power : 0.0F);
                this.power = 0.0F;
                this.setActive(false, false, true);
                chr.getInventory().AddItem(drainableComboItem);
                if (GameClient.bClient) {
                    this.syncCustomizedSettings(null);
                }

                return drainableComboItem;
            }
        }

        return null;
    }

    public boolean hasLightBulb() {
        return this.bulbItem != null;
    }

    public String getBulbItem() {
        return this.bulbItem;
    }

    public void setBulbItemRaw(String item) {
        this.bulbItem = item;
    }

    public void addLightBulb(IsoGameCharacter chr, InventoryItem bulb) {
        if (!this.hasLightBulb() && bulb != null && bulb.getType().startsWith("LightBulb")) {
            IsoLightSource isoLightSource = this.getPrimaryLight();
            if (isoLightSource != null) {
                this.setPrimaryR(bulb.getColorRed());
                this.setPrimaryG(bulb.getColorGreen());
                this.setPrimaryB(bulb.getColorBlue());
                this.bulbItem = bulb.getFullType();
                chr.removeFromHands(bulb);
                chr.getInventory().Remove(bulb);
                if (GameClient.bClient) {
                    this.syncCustomizedSettings(null);
                }
            }
        }
    }

    public InventoryItem removeLightBulb(IsoGameCharacter chr) {
        IsoLightSource isoLightSource = this.getPrimaryLight();
        if (isoLightSource != null && this.hasLightBulb()) {
            InventoryItem inventoryItem = InventoryItemFactory.CreateItem(this.bulbItem);
            if (inventoryItem != null) {
                inventoryItem.setColorRed(this.getPrimaryR());
                inventoryItem.setColorGreen(this.getPrimaryG());
                inventoryItem.setColorBlue(this.getPrimaryB());
                inventoryItem.setColor(new Color(isoLightSource.r, isoLightSource.g, isoLightSource.b));
                this.bulbItem = null;
                chr.getInventory().AddItem(inventoryItem);
                this.setActive(false, false, true);
                if (GameClient.bClient) {
                    this.syncCustomizedSettings(null);
                }

                return inventoryItem;
            }
        }

        return null;
    }

    private IsoLightSource getPrimaryLight() {
        return this.lights.size() > 0 ? (IsoLightSource)this.lights.get(0) : null;
    }

    public float getPrimaryR() {
        return this.getPrimaryLight() != null ? this.getPrimaryLight().r : this.primaryR;
    }

    public float getPrimaryG() {
        return this.getPrimaryLight() != null ? this.getPrimaryLight().g : this.primaryG;
    }

    public float getPrimaryB() {
        return this.getPrimaryLight() != null ? this.getPrimaryLight().b : this.primaryB;
    }

    public void setPrimaryR(float r) {
        this.primaryR = r;
        if (this.getPrimaryLight() != null) {
            this.getPrimaryLight().r = r;
        }
    }

    public void setPrimaryG(float g) {
        this.primaryG = g;
        if (this.getPrimaryLight() != null) {
            this.getPrimaryLight().g = g;
        }
    }

    public void setPrimaryB(float b) {
        this.primaryB = b;
        if (this.getPrimaryLight() != null) {
            this.getPrimaryLight().b = b;
        }
    }

    @Override
    public void load(ByteBuffer input, int WorldVersion, boolean IS_DEBUG_SAVE) throws IOException {
        super.load(input, WorldVersion, IS_DEBUG_SAVE);
        this.lightRoom = input.get() == 1;
        this.RoomID = input.getInt();
        this.Activated = input.get() == 1;
        if (WorldVersion >= 76) {
            this.canBeModified = input.get() == 1;
            if (this.canBeModified) {
                this.useBattery = input.get() == 1;
                this.hasBattery = input.get() == 1;
                if (input.get() == 1) {
                    this.bulbItem = zombie.GameWindow.ReadString(input);
                } else {
                    this.bulbItem = null;
                }

                this.power = input.getFloat();
                this.delta = input.getFloat();
                this.setPrimaryR(input.getFloat());
                this.setPrimaryG(input.getFloat());
                this.setPrimaryB(input.getFloat());
            }
        }

        if (WorldVersion >= 79) {
            this.lastMinuteStamp = input.getLong();
            this.bulbBurnMinutes = input.getInt();
        }

        this.bStreetLight = this.sprite != null && this.sprite.getProperties().Is("streetlight");
        if (this.square != null) {
            IsoRoom isoRoom = this.square.getRoom();
            if (isoRoom != null && this.lightRoom) {
                this.Activated = isoRoom.def.bLightsActive;
                isoRoom.lightSwitches.add(this);
            } else {
                float float0 = 0.9F;
                float float1 = 0.8F;
                float float2 = 0.7F;
                if (this.sprite != null && this.sprite.getProperties().Is("lightR")) {
                    if (WorldVersion >= 76 && this.canBeModified) {
                        float0 = this.primaryR;
                        float1 = this.primaryG;
                        float2 = this.primaryB;
                    } else {
                        float0 = Float.parseFloat(this.sprite.getProperties().Val("lightR")) / 255.0F;
                        float1 = Float.parseFloat(this.sprite.getProperties().Val("lightG")) / 255.0F;
                        float2 = Float.parseFloat(this.sprite.getProperties().Val("lightB")) / 255.0F;
                        this.primaryR = float0;
                        this.primaryG = float1;
                        this.primaryB = float2;
                    }
                }

                int _int = 8;
                if (this.sprite.getProperties().Is("LightRadius") && Integer.parseInt(this.sprite.getProperties().Val("LightRadius")) > 0) {
                    _int = Integer.parseInt(this.sprite.getProperties().Val("LightRadius"));
                }

                IsoLightSource isoLightSource = new IsoLightSource((int)this.getX(), (int)this.getY(), (int)this.getZ(), float0, float1, float2, _int);
                isoLightSource.bActive = this.Activated;
                isoLightSource.bWasActive = isoLightSource.bActive;
                isoLightSource.bHydroPowered = true;
                isoLightSource.switches.add(this);
                this.lights.add(isoLightSource);
            }

            if (zombie.SystemDisabler.doObjectStateSyncEnable && GameClient.bClient) {
                GameClient.instance.objectSyncReq.putRequestLoad(this.square);
            }
        }
    }

    @Override
    public void save(ByteBuffer output, boolean IS_DEBUG_SAVE) throws IOException {
        super.save(output, IS_DEBUG_SAVE);
        output.put((byte)(this.lightRoom ? 1 : 0));
        output.putInt(this.RoomID);
        output.put((byte)(this.Activated ? 1 : 0));
        output.put((byte)(this.canBeModified ? 1 : 0));
        if (this.canBeModified) {
            output.put((byte)(this.useBattery ? 1 : 0));
            output.put((byte)(this.hasBattery ? 1 : 0));
            output.put((byte)(this.hasLightBulb() ? 1 : 0));
            if (this.hasLightBulb()) {
                zombie.GameWindow.WriteString(output, this.bulbItem);
            }

            output.putFloat(this.power);
            output.putFloat(this.delta);
            output.putFloat(this.getPrimaryR());
            output.putFloat(this.getPrimaryG());
            output.putFloat(this.getPrimaryB());
        }

        output.putLong(this.lastMinuteStamp);
        output.putInt(this.bulbBurnMinutes);
    }

    @Override
    public boolean onMouseLeftClick(int x, int y) {
        return false;
    }

    public boolean canSwitchLight() {
        if (this.bulbItem != null) {
            boolean boolean0 = IsoWorld.instance.isHydroPowerOn();
            boolean boolean1 = boolean0 ? this.square.getRoom() != null || this.bStreetLight : this.square.haveElectricity();
            if (!boolean1 && this.getCell() != null) {
                for (int int0 = 0; int0 >= (this.getZ() >= 1.0F ? -1 : 0); int0--) {
                    for (int int1 = -1; int1 < 2; int1++) {
                        for (int int2 = -1; int2 < 2; int2++) {
                            if (int1 != 0 || int2 != 0 || int0 != 0) {
                                IsoGridSquare isoGridSquare = this.getCell()
                                    .getGridSquare(
                                        (double)(this.getX() + (float)int1), (double)(this.getY() + (float)int2), (double)(this.getZ() + (float)int0)
                                    );
                                if (isoGridSquare != null && (boolean0 && isoGridSquare.getRoom() != null || isoGridSquare.haveElectricity())) {
                                    boolean1 = true;
                                    break;
                                }
                            }
                        }

                        if (boolean1) {
                            break;
                        }
                    }
                }
            }

            if (!this.useBattery && boolean1 || this.canBeModified && this.useBattery && this.hasBattery && this.power > 0.0F) {
                return true;
            }
        }

        return false;
    }

    public boolean setActive(boolean active) {
        return this.setActive(active, false, false);
    }

    public boolean setActive(boolean active, boolean setActiveBoolOnly) {
        return this.setActive(active, setActiveBoolOnly, false);
    }

    public boolean setActive(boolean active, boolean setActiveBoolOnly, boolean ignoreSwitchCheck) {
        if (this.bulbItem == null) {
            active = false;
        }

        if (active == this.Activated) {
            return this.Activated;
        } else if (this.square.getRoom() == null && !this.canBeModified) {
            return this.Activated;
        } else {
            if (ignoreSwitchCheck || this.canSwitchLight()) {
                this.Activated = active;
                if (!setActiveBoolOnly) {
                    IsoWorld.instance.getFreeEmitter().playSound("LightSwitch", this.square);
                    this.switchLight(this.Activated);
                    this.syncIsoObject(false, (byte)(this.Activated ? 1 : 0), null);
                }
            }

            return this.Activated;
        }
    }

    public boolean toggle() {
        return this.setActive(!this.Activated);
    }

    public void switchLight(boolean _Activated) {
        if (this.lightRoom && this.square.getRoom() != null) {
            this.square.getRoom().def.bLightsActive = _Activated;

            for (int int0 = 0; int0 < this.square.getRoom().lightSwitches.size(); int0++) {
                ((IsoLightSwitch)this.square.getRoom().lightSwitches.get(int0)).Activated = _Activated;
            }

            if (GameServer.bServer) {
                int int1 = this.square.getX() / 300;
                int int2 = this.square.getY() / 300;
                int int3 = this.square.getRoom().def.ID;
                GameServer.sendMetaGrid(int1, int2, int3);
            }
        }

        for (int int4 = 0; int4 < this.lights.size(); int4++) {
            IsoLightSource isoLightSource = (IsoLightSource)this.lights.get(int4);
            isoLightSource.bActive = _Activated;
        }

        IsoGridSquare.RecalcLightTime = -1;
        zombie.GameTime.instance.lightSourceUpdate = 100.0F;
        IsoGenerator.updateGenerator(this.getSquare());
    }

    public void getCustomSettingsFromItem(InventoryItem item) {
        if (item instanceof Moveable moveable && moveable.isLight()) {
            this.useBattery = moveable.isLightUseBattery();
            this.hasBattery = moveable.isLightHasBattery();
            this.bulbItem = moveable.getLightBulbItem();
            this.power = moveable.getLightPower();
            this.delta = moveable.getLightDelta();
            this.setPrimaryR(moveable.getLightR());
            this.setPrimaryG(moveable.getLightG());
            this.setPrimaryB(moveable.getLightB());
        }
    }

    public void setCustomSettingsToItem(InventoryItem item) {
        if (item instanceof Moveable moveable) {
            moveable.setLightUseBattery(this.useBattery);
            moveable.setLightHasBattery(this.hasBattery);
            moveable.setLightBulbItem(this.bulbItem);
            moveable.setLightPower(this.power);
            moveable.setLightDelta(this.delta);
            moveable.setLightR(this.primaryR);
            moveable.setLightG(this.primaryG);
            moveable.setLightB(this.primaryB);
        }
    }

    public void syncCustomizedSettings(UdpConnection source) {
        if (GameClient.bClient) {
            this.writeCustomizedSettingsPacket(GameClient.connection);
        } else if (GameServer.bServer) {
            for (UdpConnection udpConnection : GameServer.udpEngine.connections) {
                if (source == null || udpConnection.getConnectedGUID() != source.getConnectedGUID()) {
                    this.writeCustomizedSettingsPacket(udpConnection);
                }
            }
        }
    }

    private void writeCustomizedSettingsPacket(UdpConnection udpConnection) {
        if (udpConnection != null) {
            ByteBufferWriter byteBufferWriter = udpConnection.startPacket();
            PacketTypes.PacketType.SyncCustomLightSettings.doPacket(byteBufferWriter);
            this.writeLightSwitchObjectHeader(byteBufferWriter, (byte)(this.Activated ? 1 : 0));
            byteBufferWriter.putBoolean(this.canBeModified);
            byteBufferWriter.putBoolean(this.useBattery);
            byteBufferWriter.putBoolean(this.hasBattery);
            byteBufferWriter.putByte((byte)(this.bulbItem != null ? 1 : 0));
            if (this.bulbItem != null) {
                zombie.GameWindow.WriteString(byteBufferWriter.bb, this.bulbItem);
            }

            byteBufferWriter.putFloat(this.power);
            byteBufferWriter.putFloat(this.delta);
            byteBufferWriter.putFloat(this.primaryR);
            byteBufferWriter.putFloat(this.primaryG);
            byteBufferWriter.putFloat(this.primaryB);
            PacketTypes.PacketType.SyncCustomLightSettings.send(udpConnection);
        }
    }

    private void readCustomizedSettingsPacket(ByteBuffer byteBuffer) {
        this.Activated = byteBuffer.get() == 1;
        this.canBeModified = byteBuffer.get() == 1;
        this.useBattery = byteBuffer.get() == 1;
        this.hasBattery = byteBuffer.get() == 1;
        if (byteBuffer.get() == 1) {
            this.bulbItem = zombie.GameWindow.ReadString(byteBuffer);
        } else {
            this.bulbItem = null;
        }

        this.power = byteBuffer.getFloat();
        this.delta = byteBuffer.getFloat();
        this.setPrimaryR(byteBuffer.getFloat());
        this.setPrimaryG(byteBuffer.getFloat());
        this.setPrimaryB(byteBuffer.getFloat());
    }

    public void receiveSyncCustomizedSettings(ByteBuffer bb, UdpConnection connection) {
        if (GameClient.bClient) {
            this.readCustomizedSettingsPacket(bb);
        } else if (GameServer.bServer) {
            this.readCustomizedSettingsPacket(bb);
            this.syncCustomizedSettings(connection);
        }

        this.switchLight(this.Activated);
    }

    private void writeLightSwitchObjectHeader(ByteBufferWriter byteBufferWriter, byte _byte) {
        byteBufferWriter.putInt(this.square.getX());
        byteBufferWriter.putInt(this.square.getY());
        byteBufferWriter.putInt(this.square.getZ());
        byteBufferWriter.putByte((byte)this.square.getObjects().indexOf(this));
        byteBufferWriter.putByte(_byte);
    }

    @Override
    public void syncIsoObjectSend(ByteBufferWriter b) {
        b.putInt(this.square.getX());
        b.putInt(this.square.getY());
        b.putInt(this.square.getZ());
        byte _byte = (byte)this.square.getObjects().indexOf(this);
        b.putByte(_byte);
        b.putByte((byte)1);
        b.putByte((byte)(this.Activated ? 1 : 0));
    }

    @Override
    public void syncIsoObject(boolean bRemote, byte val, UdpConnection source, ByteBuffer bb) {
        this.syncIsoObject(bRemote, val, source);
    }

    public void syncIsoObject(boolean bRemote, byte val, UdpConnection source) {
        if (this.square == null) {
            System.out.println("ERROR: " + this.getClass().getSimpleName() + " square is null");
        } else if (this.getObjectIndex() == -1) {
            System.out
                .println(
                    "ERROR: "
                        + this.getClass().getSimpleName()
                        + " not found on square "
                        + this.square.getX()
                        + ","
                        + this.square.getY()
                        + ","
                        + this.square.getZ()
                );
        } else {
            if (GameClient.bClient && !bRemote) {
                ByteBufferWriter byteBufferWriter0 = GameClient.connection.startPacket();
                PacketTypes.PacketType.SyncIsoObject.doPacket(byteBufferWriter0);
                this.syncIsoObjectSend(byteBufferWriter0);
                PacketTypes.PacketType.SyncIsoObject.send(GameClient.connection);
            } else if (bRemote) {
                if (val == 1) {
                    this.switchLight(true);
                    this.Activated = true;
                } else {
                    this.switchLight(false);
                    this.Activated = false;
                }

                if (GameServer.bServer) {
                    for (UdpConnection udpConnection : GameServer.udpEngine.connections) {
                        if (source != null) {
                            if (udpConnection.getConnectedGUID() != source.getConnectedGUID()) {
                                ByteBufferWriter byteBufferWriter1 = udpConnection.startPacket();
                                PacketTypes.PacketType.SyncIsoObject.doPacket(byteBufferWriter1);
                                this.syncIsoObjectSend(byteBufferWriter1);
                                PacketTypes.PacketType.SyncIsoObject.send(udpConnection);
                            }
                        } else if (udpConnection.RelevantTo((float)this.square.x, (float)this.square.y)) {
                            ByteBufferWriter byteBufferWriter2 = udpConnection.startPacket();
                            PacketTypes.PacketType.SyncIsoObject.doPacket(byteBufferWriter2);
                            byteBufferWriter2.putInt(this.square.getX());
                            byteBufferWriter2.putInt(this.square.getY());
                            byteBufferWriter2.putInt(this.square.getZ());
                            byte _byte = (byte)this.square.getObjects().indexOf(this);
                            if (_byte != -1) {
                                byteBufferWriter2.putByte(_byte);
                            } else {
                                byteBufferWriter2.putByte((byte)this.square.getObjects().size());
                            }

                            byteBufferWriter2.putByte((byte)1);
                            byteBufferWriter2.putByte((byte)(this.Activated ? 1 : 0));
                            PacketTypes.PacketType.SyncIsoObject.send(udpConnection);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void update() {
        if (!GameServer.bServer && !GameClient.bClient || GameServer.bServer) {
            boolean boolean0 = false;
            if (!this.Activated) {
                this.lastMinuteStamp = -1L;
            }

            if (!this.lightRoom && this.canBeModified && this.Activated) {
                if (this.lastMinuteStamp == -1L) {
                    this.lastMinuteStamp = zombie.GameTime.instance.getMinutesStamp();
                }

                if (zombie.GameTime.instance.getMinutesStamp() > this.lastMinuteStamp) {
                    if (this.bulbBurnMinutes == -1) {
                        int int0 = zombie.SandboxOptions.instance.getElecShutModifier() * 24 * 60;
                        if (this.lastMinuteStamp < (long)int0) {
                            this.bulbBurnMinutes = (int)this.lastMinuteStamp;
                        } else {
                            this.bulbBurnMinutes = int0;
                        }
                    }

                    long _long = zombie.GameTime.instance.getMinutesStamp() - this.lastMinuteStamp;
                    this.lastMinuteStamp = zombie.GameTime.instance.getMinutesStamp();
                    boolean boolean1 = false;
                    boolean boolean2 = IsoWorld.instance.isHydroPowerOn();
                    boolean boolean3 = boolean2 ? this.square.getRoom() != null : this.square.haveElectricity();
                    boolean boolean4 = this.useBattery && this.hasBattery && this.power > 0.0F;
                    if (boolean4 || !this.useBattery && boolean3) {
                        boolean1 = true;
                    }

                    double _double = zombie.SandboxOptions.instance.LightBulbLifespan.getValue();
                    if (_double <= 0.0) {
                        boolean1 = false;
                    }

                    if (this.Activated && this.hasLightBulb() && boolean1) {
                        this.bulbBurnMinutes = (int)((long)this.bulbBurnMinutes + _long);
                    }

                    this.nextBreakUpdate = (int)((long)this.nextBreakUpdate - _long);
                    if (this.nextBreakUpdate <= 0) {
                        if (this.Activated && this.hasLightBulb() && boolean1) {
                            int int1 = (int)(1000.0 * _double);
                            if (int1 < 1) {
                                int1 = 1;
                            }

                            int int2 = Rand.Next(0, int1);
                            int int3 = this.bulbBurnMinutes / 10000;
                            if (int2 < int3) {
                                this.bulbBurnMinutes = 0;
                                this.setActive(false, true, true);
                                this.bulbItem = null;
                                IsoWorld.instance.getFreeEmitter().playSound("LightbulbBurnedOut", this.square);
                                boolean0 = true;
                                if (Core.bDebug) {
                                    System.out.println("broke bulb at x=" + this.getX() + ", y=" + this.getY() + ", z=" + this.getZ());
                                }
                            }
                        }

                        this.nextBreakUpdate = 60;
                    }

                    if (this.Activated && boolean4 && this.hasLightBulb()) {
                        float _float = this.power - this.power % 0.01F;
                        this.power = this.power - this.delta * (float)_long;
                        if (this.power < 0.0F) {
                            this.power = 0.0F;
                        }

                        if (_long == 1L || this.power < _float) {
                            boolean0 = true;
                        }
                    }
                }

                if (this.useBattery && this.Activated && (this.power <= 0.0F || !this.hasBattery)) {
                    this.power = 0.0F;
                    this.setActive(false, true, true);
                    boolean0 = true;
                }
            }

            if (this.Activated && !this.hasLightBulb()) {
                this.setActive(false, true, true);
                boolean0 = true;
            }

            if (boolean0 && GameServer.bServer) {
                this.syncCustomizedSettings(null);
            }
        }
    }

    public boolean isActivated() {
        return this.Activated;
    }

    @Override
    public void addToWorld() {
        if (!this.Activated) {
            this.lastMinuteStamp = -1L;
        }

        if (!this.lightRoom && !this.lights.isEmpty()) {
            for (int _int = 0; _int < this.lights.size(); _int++) {
                IsoWorld.instance.CurrentCell.getLamppostPositions().add((IsoLightSource)this.lights.get(_int));
            }
        }

        if (this.getCell() != null && this.canBeModified && !this.lightRoom && (!GameServer.bServer && !GameClient.bClient || GameServer.bServer)) {
            this.getCell().addToStaticUpdaterObjectList(this);
        }

        this.checkAmbientSound();
    }

    @Override
    public void removeFromWorld() {
        if (!this.lightRoom && !this.lights.isEmpty()) {
            for (int _int = 0; _int < this.lights.size(); _int++) {
                ((IsoLightSource)this.lights.get(_int)).setActive(false);
                IsoWorld.instance.CurrentCell.removeLamppost((IsoLightSource)this.lights.get(_int));
            }

            this.lights.clear();
        }

        if (this.square != null && this.lightRoom) {
            IsoRoom isoRoom = this.square.getRoom();
            if (isoRoom != null) {
                isoRoom.lightSwitches.remove(this);
            }
        }

        super.removeFromWorld();
    }

    public static void chunkLoaded(IsoChunk chunk) {
        ArrayList arrayList = new ArrayList();

        for (int int0 = 0; int0 < 10; int0++) {
            for (int int1 = 0; int1 < 10; int1++) {
                for (int int2 = 0; int2 < 8; int2++) {
                    IsoGridSquare isoGridSquare = chunk.getGridSquare(int0, int1, int2);
                    if (isoGridSquare != null) {
                        IsoRoom isoRoom0 = isoGridSquare.getRoom();
                        if (isoRoom0 != null && isoRoom0.hasLightSwitches() && !arrayList.contains(isoRoom0)) {
                            arrayList.add(isoRoom0);
                        }
                    }
                }
            }
        }

        for (int int3 = 0; int3 < arrayList.size(); int3++) {
            IsoRoom isoRoom1 = (IsoRoom)arrayList.get(int3);
            isoRoom1.createLights(isoRoom1.def.bLightsActive);

            for (int int4 = 0; int4 < isoRoom1.roomLights.size(); int4++) {
                IsoRoomLight isoRoomLight = (IsoRoomLight)isoRoom1.roomLights.get(int4);
                if (!chunk.roomLights.contains(isoRoomLight)) {
                    chunk.roomLights.add(isoRoomLight);
                }
            }
        }
    }

    public ArrayList<IsoLightSource> getLights() {
        return this.lights;
    }
}
